package com.common.base.utils

import android.os.Parcelable
import com.google.gson.Gson

/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：26/8/2025
 *
 * 描述：
 *
 * 修订历史：
 *
 */
object SpUtilsManagement {

    val managementType = 0//0:SharedPreferences    1:MMKV

    // =====================Boolean=====================
    fun putBoolean(key: String, value: Boolean) {
        SpUtils.putBoolean(key, value)
        SPUtil.save(key, value)
    }

    fun getBoolean(key: String, defValue: Boolean = false): Boolean {
        return try {
            when(managementType){
                1-> SpUtils.getBoolean(key, defValue)
                else -> SPUtil.getBoolean(key, defValue)
            }
        } catch (e: ClassCastException) {
            // 如果类型不匹配，返回默认值并清除旧数据
            putBoolean(key, defValue)
            defValue
        }
    }

    // =====================String=====================
    fun putString(key: String, value: String) {
        SpUtils.putString(key, value)
        SPUtil.save(key, value)
    }

    fun getString(key: String, defValue: String = ""): String {
        return try {
            when(managementType){
                1-> SpUtils.getString(key, defValue)
                else -> SPUtil.getString(key, defValue) ?:defValue
            }
        } catch (e: ClassCastException) {
            // 如果类型不匹配，返回默认值并清除旧数据
            putString(key, defValue)
            defValue
        }
    }

    // =====================Parcelable=====================
    fun putParcelable(key: String, value: Parcelable) {
        SpUtils.putParcelable(key, value)
        SPUtil.save(key, value)
    }

    // 推荐的reified版本
    inline fun <reified T : Parcelable> getParcelable(key: String): T? {
        return when(managementType){
            1-> SpUtils.getParcelable(key, T::class.java)
            else -> SPUtil.getParcelable<T>(key)
        }
    }

    // 兼容现有代码的Class版本
    fun <T : Parcelable> getParcelable(key: String, clazz: Class<T>): T? {
        return when(managementType){
            1-> SpUtils.getParcelable(key, clazz)
            else -> {
                val jsonString = SPUtil.getString(key)
                if (jsonString.isNotEmpty()) {
                    try {
                        Gson().fromJson(jsonString, clazz)
                    } catch (e: Exception) {
                        null
                    }
                } else {
                    null
                }
            }
        }
    }


    // =====================Int=====================
    fun putInt(key: String, value: Int) {
        SpUtils.putInt(key, value)
        SPUtil.save(key, value)
    }

    fun getInt(key: String, defValue: Int = 0): Int {
        return try {
            when(managementType){
                1-> SpUtils.getInt(key, defValue)
                else -> SPUtil.getInt(key, defValue)
            }
        } catch (e: ClassCastException) {
            // 如果类型不匹配，返回默认值并清除旧数据
            putInt(key, defValue)
            defValue
        }
    }

    // =====================Long=====================
    fun putLong(key: String, value: Long) {
        SpUtils.putLong(key, value)
        SPUtil.saveLong(key, value)
    }

    fun getLong(key: String, defValue: Long = 0): Long {
        return try {
            when(managementType){
                1-> SpUtils.getLong(key, defValue)
                else -> SPUtil.getLong(key, defValue)
            }
        } catch (e: ClassCastException) {
            // 如果类型不匹配，返回默认值并清除旧数据
            putLong(key, defValue)
            defValue
        }
    }


    // =====================Float=====================
    fun putFloat(key: String, value: Float) {
        SpUtils.putFloat(key, value)
        SPUtil.save(key, value)
    }

    fun getFloat(key: String, defValue: Float = 0f): Float {
        return try {
            when(managementType){
                1-> SpUtils.getFloat(key, defValue)
                else -> SPUtil.getFloat(key, defValue)
            }
        } catch (e: ClassCastException) {
            // 如果类型不匹配，返回默认值并清除旧数据
            putFloat(key, defValue)
            defValue
        }
    }

    // =====================Double=====================
    fun putDouble(key: String, value: Double) {
        SpUtils.putDouble(key, value)
        SPUtil.save(key, value)
    }

    fun getDouble(key: String, defValue: Double = 0.0): Double {
        return try {
            when(managementType){
                1-> SpUtils.getDouble(key, defValue)
                else -> SPUtil.getDouble(key, defValue)
            }
        } catch (e: ClassCastException) {
            // 如果类型不匹配，返回默认值并清除旧数据
            putDouble(key, defValue)
            defValue
        }
    }

    fun removeFromKey(key: String) {
        SpUtils.removeFromKey(key)
        SPUtil.remove(key)
    }
}